/**
 * 
 */
package com.scpii.universal.download;

import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.net.URLDecoder;
import java.util.Properties;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.io.ResourceLoader;

import com.scpii.universal.util.Const;

/**
 * UniversalApi
 * 
 * @author 甘建新 2012-10-18上午11:30:46
 */
public class AppDownLoadServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private Logger log = Logger.getLogger(AppDownLoadServlet.class);

	public AppDownLoadServlet() {
		super();
	}

	public void destroy() {
		super.destroy();
	}

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		log.info("开始下载");
		String osTypeStr = request.getParameter("ostype");
		String redirectUrl = "";
		int osType = 9;
		if (osTypeStr != null) {
			osType = Integer.parseInt(osTypeStr);
			redirectUrl = request.getParameter("appurl");
			redirectUrl = URLDecoder.decode(redirectUrl, "utf-8");
			if (osType == Const.OS_ANDROID) {
				response.sendRedirect(redirectUrl);
			} else if (osType == Const.OS_IOS) {
				PrintWriter out=response.getWriter();
				String result="<script type='text/javascript'>" +
						"window.location='"+redirectUrl+"'" +
						"</script>";		
				out.print(result);
				out.flush();
				out.close();
			}
		}
		System.out.println(redirectUrl);
		log.info("跳转下载页面地址为:" + redirectUrl);
//		response.sendRedirect(redirectUrl);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
	}

	public void init() throws ServletException {
		// 载入配置文件
		log.info("载入config.properties配置文件");
		String path = getServletContext().getInitParameter("config");
		Properties properties = new Properties();
		try {
			ResourceLoader resourceLoader = new DefaultResourceLoader();
			InputStream is = resourceLoader.getResource(path).getInputStream();
			properties.load(is);
			String imgPath = properties.getProperty("HOST_SRC_IMG_PATH");
			String appDownPath = properties
					.getProperty("HOST_APP_DOWNLOAD_PATH");

			if ((imgPath != null && imgPath.trim().length() > 0)
					&& (appDownPath != null && appDownPath.trim().length() > 0)) {
				Const.HOST_SRC_IMG_PATH = imgPath;
				Const.HOST_APP_DOWNLOAD_PATH = appDownPath;
			}

			String msgAppNotUpdate = properties.getProperty("APP_UPDATE_NOT");
			String msgAppMustUpdae = properties.getProperty("APP_UPDATE_MUST");
			String msgAppCanUpdate = properties.getProperty("APP_UPDATE_CAN");

			Const.MSG_APP_NOT_UPDATE = msgAppNotUpdate;
			Const.MSG_APP_MUST_UPDATE = msgAppMustUpdae;
			Const.MSG_APP_CAN_UPDATE = msgAppCanUpdate;

		} catch (Exception e) {
			e.printStackTrace();
			log.error("载入config.properties配置文件异常：", e);
		}
	}
}
